// (c) Copyright HutongGames, LLC 2010-2013. All rights reserved.

using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
	[ActionCategory(ActionCategory.StateMachine)]
	[ActionTarget(typeof(PlayMakerFSM), "gameObject,fsmName")]
	[Tooltip("[Custom] Get the value of a global integer variable.")]
	public class GetGlobalInt : FsmStateAction
	{
		[RequiredField]
		[UIHint(UIHint.Variable)]
		[Tooltip("The global int variable.")]
		public FsmInt globalIntVariable;

		[RequiredField]
		[UIHint(UIHint.Variable)]
		[Tooltip("Store the value in an Int variable in this FSM.")]
		public FsmInt storeValue;

		[Tooltip("Repeat every frame. Useful if the value is changing.")]
		public bool everyFrame;

		public override void Reset()
		{
			storeValue = null;
		}

		public override void OnEnter()
		{
			DoGetGlobalInt();

			if (!everyFrame)
				Finish();
		}

		public override void OnUpdate()
		{
			DoGetGlobalInt();
		}

		private void DoGetGlobalInt()
		{
			if (storeValue == null) return;
			if (globalIntVariable == null) return;
			storeValue.Value = globalIntVariable.Value;
		}
	}
}